﻿#include "ExteriorTest.h"
#include "DatabaseMgmt.h"

CExteriorTest::CExteriorTest()
{

}

CExteriorTest::~CExteriorTest()
{

}

int CExteriorTest::insertRow(ExteriorTest data)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("INSERT INTO TB_EXTERIOR_TEST (REPORT_UID,INCLUSION,UNBREAKABLE)"
		" VALUES (%1,'%2','%3');")
		.arg(data.report_uid)
		.arg(data.inclusion)
		.arg(data.unbreakable);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		qInfo() << query.lastError().text();
		ret = CONST_ERR_EXEC_SQL_FAILED;
	}
	return ret;
}

int CExteriorTest::removeByReportUid(uint32_t report_id)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_EXTERIOR_TEST WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CExteriorTest::modifyRowByUid(ExteriorTest data)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("UPDATE TB_EXTERIOR_TEST SET REPORT_UID=%1,INCLUSION='%2',UNBREAKABLE='%3'"
		" WHERE UID=%4")
		.arg(data.report_uid)
		.arg(data.inclusion)
		.arg(data.unbreakable)
		.arg(data.uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CExteriorTest::queryByReportUid(uint32_t report_id, ExteriorTest& data)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT UID,REPORT_UID,INCLUSION,UNBREAKABLE"
		" FROM TB_EXTERIOR_TEST WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}
	else
	{
		if (query.next())
		{
			data.uid = query.value("UID").toUInt();
			data.report_uid = query.value("REPORT_UID").toUInt();
			data.inclusion = query.value("INCLUSION").toString();
			data.unbreakable = query.value("UNBREAKABLE").toString();
		}
	}

	return ret;
}
